package com.jk.sec.service.Impl;

import com.jk.*;
import com.jk.sec.dao.MovieDao;
import com.jk.sec.service.MovieService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.logging.SimpleFormatter;

@Service
public class MovieServiceImpl implements MovieService {

    @Autowired
    private MovieDao movieDao;
    @Autowired
    private HttpSession session;
/**
 * @Author: 孙尔超
 * @Description: 后台系统左侧树
 * @Date: 2021/5/19 17:34
 * @Return: java.util.List<com.jk.TreeBean>
 **/
    @Override
    public List<TreeBean> findTree() {
        Integer pid=0;
        List<TreeBean> list = findTreePid(pid);
        return list;
    }



    /**
     * @Author: 孙尔超
     * @Description: 递归树自己调自己
     * @Date: 2021/5/19 17:34
     * @param pid
     * @Return: java.util.List<com.jk.TreeBean>
     **/
    private List<TreeBean> findTreePid(Integer pid) {
        List<TreeBean> list=movieDao.findTreePid(pid);
        for (TreeBean treeBean : list) {
            Integer id = treeBean.getId();
            List<TreeBean> list1=findTreePid(id);
            if (list1!=null && list1.size()>0){
                treeBean.setSelectable(false);
                treeBean.setNodes(list1);
            }else{
                treeBean.setSelectable(true);
            }
        }
        return list;
    }

    /**
     * @Author: 孙尔超
     * @Description: 后台登录系统
     * @Date: 2021/5/19 17:34
     * @param user
     * @Return: java.lang.String
     **/
    @Override
    public HashMap login(CinemaBean user,String getcode) {

        HashMap<String, Object> map = new HashMap<>();
        try {
            // 生成一个MD5加密计算摘要
            MessageDigest md = MessageDigest.getInstance("MD5");
            // 计算md5函数
            md.update(user.getPwd().getBytes());
            // digest()最后确定返回md5 hash值，返回值为8位字符串。因为md5 hash值是16位的hex值，实际上就是8位的字符
            // BigInteger函数则将8位的字符串转换成16位hex值，用字符串来表示；得到字符串形式的hash值
            //一个byte是八位二进制，也就是2位十六进制字符（2的8次方等于16的2次方）
            String s = new BigInteger(1, md.digest()).toString(16);
            user.setPwd(s);
        } catch (Exception e) {
            e.printStackTrace();
        }

        CinemaBean user1=movieDao.login(user.getName());

        if (user1==null){
            map.put("info","用户名不存在");
            return map;
        }
        if (!user.getPwd().equals(user1.getPwd())){
            map.put("info","密码错误");
            return map;
        }
        session.setAttribute("user",user1);

        if (user1.getRole() == 1){
            map.put("info","登录成功");
            map.put("role",1);
            map.put("url","/tree");
        }else{
            map.put("info","登录成功");
            map.put("role",0);
            map.put("url","/roleTree");
        }
        return map;
    }
        /**
         * @Author: 孙尔超
         * @Description: 注册商家用户
         * @Date: 2021/5/19 20:44
         * @param user
         * @Return: void
         **/
    @Override
    public Map addUser(CinemaBean user) {
        HashMap<Object, Object> map = new HashMap<>();

        List<CinemaBean> list=movieDao.queryUser();
        //判断是用户否存在
        for (CinemaBean userlist : list) {
            if (userlist.getName().equals(user.getName())){
                map.put("info","用户名已存在");
                return map;
            }
        }
        if (user.getName()==""){
            map.put("info","用户名为空");
            return map;
        }else {
        if (user.getConfirm().equals(user.getPwd())&&user.getName()!=null&&user.getName()!=""&&user.getPwd()!=null&&user.getPwd()!=""){
            try {
                // 生成一个MD5加密计算摘要
                MessageDigest md = MessageDigest.getInstance("MD5");
                // 计算md5函数
                md.update(user.getPwd().getBytes());
                // digest()最后确定返回md5 hash值，返回值为8位字符串。因为md5 hash值是16位的hex值，实际上就是8位的字符
                // BigInteger函数则将8位的字符串转换成16位hex值，用字符串来表示；得到字符串形式的hash值
                //一个byte是八位二进制，也就是2位十六进制字符（2的8次方等于16的2次方）
                String s = new BigInteger(1, md.digest()).toString(16);
                user.setPwd(s);
            } catch (Exception e) {
                e.printStackTrace();
            }
            movieDao.addUser(user);
            map.put("info","注册成功");
            return map;
        }
        else {
            map.put("info","两次密码不一致");
        }
        }
        return map;
    }

    /**
     * @Author: 孙尔超
     * @Description: 分页查询影片
     * @Date: 2021/5/20 16:18
     * @param page
     * @param rows
     * @param movie
     * @Return: java.util.Map
     **/
    @Override
    public Map findPicture(Integer page, Integer rows, MovieBean movie) {
        CinemaBean cinema = (CinemaBean) session.getAttribute("user");
        movie.setMovieAccruing(cinema.getCinemaId());
        Integer count=movieDao.count(movie);
        Integer start=(page-1)*rows;
        List<MovieBean> list=movieDao.findPicture(start,rows,movie);
        HashMap<String, Object> map=new HashMap<String, Object>();
        map.put("total",count);
        map.put("rows",list);
        return map;
    }

   /**
    * @Author: 孙尔超
    * @Description: 删除影片
    * @Date: 2021/5/20 16:18
    * @param ids
    * @Return: void
    **/
    @Override
    public void del(Integer ids) {
        CinemaBean cinema = (CinemaBean) session.getAttribute("user");
        movieDao.del(ids);//删除影片
        movieDao.delRole(ids);//删除影片演员中间表
        movieDao.delmovierole(ids);//删除影片演员表
        movieDao.delmovietype(ids);//删除类型中间表
    }

    /**
     * @Author: 孙尔超
     * @Description:动态查询下拉列表框
     * @Date: 2021/5/20 16:28
     * @Return: java.util.List<com.jk.MovieTypeBean>
     **/
    @Override
    public List<MovieTypeBean> movieType() {
        return movieDao.movieType();
    }

    @Override
    public void addMovie(MovieBean movie) throws ParseException {
        CinemaBean cinema = (CinemaBean) session.getAttribute("user");
        if (movie.getMovieId()==null){
            SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd");
            if (sim.parse(movie.getFilmSchedule()).getTime()<=new Date().getTime()){
                movie.setStatus(1);//状态为上映
            }else {
                movie.setStatus(2);//状态为未上映
            }
            movie.setScore(0);
            movie.setMovieGrade(0.0);
            int i  = (int) Math.ceil(Math.random() * 899 + 100);
            movie.setMovieAccruing(i);
            movie.setLook(0);
            movieDao.addMovie(movie);//新增影片表


        }else{
            movieDao.updateMovie(movie);//修改影片表
            movieDao.delmovietype(movie.getMovieId());//先删除类型中间表
            movieDao.delmovierole(movie.getMovieId());//先删除角色表
            movieDao.delRole(movie.getMovieId());//先删除角色中间表
        }
        if (movie.getTypeids()!=null &&movie.getTypeids()!=""){
            String[] split = movie.getTypeids().split(",");
            movieDao.addmovietypeid(split,movie.getMovieId());//在新增类型中间表
        }
        movieDao.addrole(movie.getRoleList());//新增演员表
        movieDao.addMovierole(movie);//新增演员中间表表
    }


    /**
     * @Author: 孙尔超
     * @Description: 回显
     * @Date: 2021/5/28 14:42
     * @param ids
     * @Return: com.jk.MovieBean
     **/
    @Override
    public Map movieQuery(Integer ids) {
        CinemaBean cinema = (CinemaBean) session.getAttribute("user");
        HashMap<Object, Object> map = new HashMap<>();
        MovieBean movie=movieDao.movieQuery(ids);//查询主表影片表
        List<ActorBean> list= movieDao.queryRole(ids);//查询多条角色表
        map.put("actor",list);//将演员存进map里面
        List<TypeBean> list2= movieDao.queryType(ids);//查询多条类型表
        map.put("movie",movie);//将影片存进map里面
        map.put("type",list2);//将类型存进map里面
        return map;
    }
/**
 * @Author: 孙尔超
 * @Description: 二级联动
 * @Date: 2021/6/1 11:29
 * @param pid
 * @Return: com.jk.AddressBean
 **/
    @Override
    public List<CinemaBean> queryCity(Integer pid) {
        return movieDao.queryCity(pid);
    }
/**
 * @Author: 孙尔超
 * @Description: 个人中心回显
 * @Date: 2021/6/1 11:29
 * @param ids
 * @Return: com.jk.CinemaBean
 **/
    @Override
    public CinemaBean cinemaQuery(Integer ids) {
        return movieDao.cinemaQuery(ids);
    }
/**
 * @Author: 孙尔超
 * @Description:
 * @Date: 2021/6/1 11:32
 * @param cinema
 * @Return: void
 **/
    @Override
    public void addCinema(CinemaBean cinema) {
        if (cinema.getCinemaId()!=null){
            movieDao.addCinema(cinema);
        }
    }

    /**
    * @Author: wh
    * @Description:
    * @Date: 2021/6/8 19:27
    * @Param:[page, rows, movie]
    * @Return: java.util.Map
    **/
    @Override
    public Map findPictureAll(Integer page, Integer rows, MovieBean movie) {
        Integer count=movieDao.count(movie);
        Integer start=(page-1)*rows;
        List<MovieBean> list=movieDao.findPicture(start,rows,movie);
        HashMap<String, Object> map=new HashMap<String, Object>();
        map.put("total",count);
        map.put("rows",list);
        return map;
    }


}
